package apibasej.shared.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class UtilDigest {

	public static void main(String[] args) throws NoSuchAlgorithmException {
		System.out.println( getSHA1HexLowerCase("teste".getBytes()) );
		System.out.println( getSHA1HexUpperCase("teste".getBytes()) );
		System.out.println( getSHA1HexUpperCase("teste".getBytes()).length() );
		System.out.println( getMD5HexLowerCase("teste".getBytes()) );
		System.out.println( getMD5HexUpperCase("teste".getBytes()) );
		System.out.println( getMD5HexUpperCase("teste".getBytes()).length() );
		//2e6f9b0d5885b6010f9167787445617f553a735f
		//2E6F9B0D5885B6010F9167787445617F553A735F
		//40
		//698dc19d489c4e4db73e28a713eab07b
		//698DC19D489C4E4DB73E28A713EAB07B
		//32
	}
	
	// retorna 40 caracteres
	public static String getSHA1HexLowerCase(byte[] bytes) throws NoSuchAlgorithmException{
		byte[] hash = getHash("SHA-1",bytes);
		return UtilCodec.encodeHexLowerCase(hash);
	}
	public static String getSHA1HexUpperCase(byte[] bytes) throws NoSuchAlgorithmException{
		byte[] hash = getHash("SHA-1",bytes);
		return UtilCodec.encodeHexUpperCase(hash);
	}
	public static String getMD5HexLowerCase(byte[] bytes) throws NoSuchAlgorithmException{
		byte[] hash = getHash("MD5",bytes);
		return UtilCodec.encodeHexLowerCase(hash);
	}
	public static String getMD5HexUpperCase(byte[] bytes) throws NoSuchAlgorithmException{
		byte[] hash = getHash("MD5",bytes);
		return UtilCodec.encodeHexUpperCase(hash);
	}
	public static byte[] getHash(String algorithm , byte[] bytes) throws NoSuchAlgorithmException{
		return MessageDigest.getInstance(algorithm).digest(bytes);
	}
	
}
